﻿Public Class ConfigPackageDetail
    Inherits System.Web.UI.Page

    Private Property DataSource As Data.DataTable

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If Request.QueryString("action") = "ADD" Then
                lblAction.Text = "เพิ่มข้อมูลการ Configuration ค่า Package"
            ElseIf Request.QueryString("action") = "EDIT" Then
                lblAction.Text = "เปลี่ยนแปลงแก้ไขข้อมูลการ Configuration ค่า Package"
                txtPackageCode.Enabled = False
            End If
            If Request.QueryString("packageid") <> "" Then
                Dim dt As DataTable = New DataTable
                Dim info As Entity.PackageItem = New Entity.PackageItem
                info = BL.Configuration.GetPackageById(Request.QueryString("packageid"))
                If Not IsNothing(info) Then
                    If Not IsNothing(info.package_id) Then txtPackageCode.Text = info.package_id
                    If Not IsNothing(info.package_rv) Then txtPackageRVValue.Text = info.package_rv
                    If Not IsNothing(info.package_amount) Then txtPackageAmount.Text = info.package_amount
                    If Not IsNothing(info.package_desc) Then txtPackageDesc.Text = info.package_desc
                    If Not IsNothing(info.record_status) Then ddlPackageStatus.SelectedValue = info.record_status
                End If
            End If
        End If
        If Request.QueryString("resultfrom") = "ADD" Then Me.Master.Alert("SUCCESS\nทำการบันทึก Package สำเร็จ")
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
        Dim SaveFlag As Boolean = True
        If txtPackageCode.Text.Trim = "" Then
            Me.Master.Alert("Package Code ห้ามเป็นค่าว่าง\nกรุงณาระบุ Package Code!")
            Exit Sub
        End If
        If txtPackageRVValue.Text.Trim = "" Then
            Me.Master.Alert("PV Value ห้ามเป็นค่าว่าง\nกรุงณาระบุ PV Value!")
            Exit Sub
        End If
        If txtPackageAmount.Text.Trim = "" Then
            Me.Master.Alert("Package Amount ห้ามเป็นค่าว่าง\nกรุงณาระบุ Package Amount!")
            Exit Sub
        End If
        If txtPackageDesc.Text.Trim = "" Then
            Me.Master.Alert("Package Description ห้ามเป็นค่าว่าง\nกรุงณาระบุ Package Description!")
            Exit Sub
        End If

        If SaveFlag Then
            Dim info As Entity.PackageItem = New Entity.PackageItem
            info.package_id = txtPackageCode.Text.Trim
            info.package_rv = Convert.ToDecimal(txtPackageRVValue.Text.Trim)
            info.package_amount = Convert.ToDecimal(txtPackageAmount.Text.Trim)
            info.package_desc = txtPackageDesc.Text.Trim
            info.record_status = ddlPackageStatus.SelectedValue
            info.create_user_id = GlobalApp.Authen.offcier_id
            info.create_date = Common.UtilityDateTime.ConvertDate(Now.ToString("dd/MM/yyyy", Common.Utility.CulInfo)).Value
            info.upd_user_id = GlobalApp.Authen.offcier_id
            info.upd_date = Common.UtilityDateTime.ConvertDate(Now.ToString("dd/MM/yyyy", Common.Utility.CulInfo)).Value
            If Request.QueryString("action") = "ADD" Then
                If BL.Configuration.InsertPackage(info) Then
                    Response.Redirect("~/ConfigPackageDetail.aspx?resultfrom=ADD&action=EDIT&packageid=" & txtPackageCode.Text.Trim, False)
                Else
                    Me.Master.Alert("FAIL!\nทำการบันทึก Package สำเร็จ")
                End If
            ElseIf Request.QueryString("action") = "EDIT" Then
                If BL.Configuration.UpdatePackage(info) Then
                    Me.Master.Alert("SUCCESS\nทำการแก้ไข Package สำเร็จ")
                Else
                    Me.Master.Alert("FAIL!\nแก้ไขรายการ Package ไม่สำเร็จ")
                End If
            End If
        End If
    End Sub

    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnReset.Click
        Me.Response.Redirect("~/ConfigPackage.aspx", False)
    End Sub

End Class